//
//  AppDelegate.m
//  jinwang
//
//  Created by jzb on 2018/2/24.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "AppDelegate.h"
#import "BaseNavigationC.h"
#import <AVFoundation/AVFoundation.h>
#import "LoginViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    /**
     IQKeyboardManager
     */
    [IQKeyboardManager sharedManager].enable = YES;//自动键盘处理事件在整个项目内是否启用。
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;//需要当键盘弹起时，点击背景收起键盘
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;//需要支持内联编辑(Inline Editing), 这就需要隐藏键盘上的工具条(默认打开)
    
    [WXApi registerApp:kWechatAPPID];
    
    //后台播放音频设置
    
    self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.window.backgroundColor = [UIColor whiteColor];
    BOOL isShowGuide = [self isShowUserGuideLoad];
    if (!isShowGuide) {
        //进入首页
        MainTabBarController *tabBarC = [[MainTabBarController alloc]init];
        self.window.rootViewController = tabBarC;
    }else{
        //进入新手引导页
        CMNewFeaturesController *vc = [[CMNewFeaturesController alloc] init];
        //        [vc configUI];
        vc.featuresArray = @[@"回车键引导页1.jpg",
                             @"回车键引导页2.jpg",
                             @"回车键引导页3.jpg"];
        [vc.experienceBtn setTitle:@"立即体验" forState:UIControlStateNormal];
        vc.currentPageIndicatorTintColor = k_themeColor;
        vc.pageIndicatorTintColor = [UIColor lightGrayColor];
        __weak typeof(self) weakSelf = self;
        vc.experienceBtnBlock = ^{
            //进入首页
            MainTabBarController *tabBarC = [[MainTabBarController alloc]init];
            weakSelf.window.rootViewController = tabBarC;
        };
        self.window.rootViewController = vc;
    }
    
    [self.window makeKeyAndVisible];
//
//    EMOptions *options = [EMOptions optionsWithAppkey:kEMKey];
//    options.apnsCertName = kApnsCertName;
//    [[EMClient sharedClient] initializeSDKWithOptions:options];
    
    [[EaseSDKHelper shareHelper] hyphenateApplication:application
                        didFinishLaunchingWithOptions:launchOptions
                                               appkey:kEMKey
                                         apnsCertName:kApnsCertName
                                          otherConfig:@{kSDKConfigEnableConsoleLogger:[NSNumber numberWithBool:YES]}];
    
//    [ChatUIHelper shareHelper];// 初始化环信相关回调
    
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        //注册推送, 用于iOS8以及iOS8之后的系统
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
        [application registerUserNotificationSettings:settings];
    } else {
        //注册推送，用于iOS8之前的系统
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }
    
    
   
    return YES;
}

#pragma mark -判断版本号
//判断版本号（是否需要展示引导页）
- (BOOL)isShowUserGuideLoad{
    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *lastRunVersion = [defaults objectForKey:LAST_RUN_VERSION_KEY];
    if (!lastRunVersion) {
        [defaults setObject:currentVersion forKey:LAST_RUN_VERSION_KEY];
        [defaults synchronize];
        return YES;
    }else if (![lastRunVersion isEqualToString:currentVersion]) {
        [defaults setObject:currentVersion forKey:LAST_RUN_VERSION_KEY];
        [defaults synchronize];
        return YES;
    }else{
        return NO;
    }
}

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
    }
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerForRemoteNotifications];
}
// 将得到的deviceToken传给SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[EMClient sharedClient] bindDeviceToken:deviceToken];
    });
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    [[EMClient sharedClient] applicationDidEnterBackground:application];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    [[EMClient sharedClient] applicationWillEnterForeground:application];
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
